package com.easyexcel;

import com.alibaba.excel.EasyExcelFactory;
import com.alibaba.excel.ExcelReader;
import com.alibaba.excel.metadata.Sheet;
import com.easyexcel.domain.Model;
import com.easyexcel.listener.ExcelListener;
import com.easyexcel.util.FileUtil;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

@RunWith(SpringRunner.class)
@SpringBootTest
public class EasyexcelDemoApplicationTestRead {
//    @Test
    public void simpleReadListStringV2003() throws IOException {
        InputStream inputStream = FileUtil.getResourcesFileInputStream("ceshi.xls");
        List<Object> data = EasyExcelFactory.read(inputStream, new Sheet(1, 0));
        inputStream.close();
        print(data);
    }
//    @Test
    public void simpleReadJavaModelV2003() throws IOException {
        InputStream inputStream = FileUtil.getResourcesFileInputStream("ceshi.xls");
        List<Object> data = EasyExcelFactory.read(inputStream, new Sheet(1, 1, Model.class));
        inputStream.close();
        print(data);
    }
//    @Test
    public void saxReadJavaModelV2003() throws IOException {
        InputStream inputStream = FileUtil.getResourcesFileInputStream("ceshi.xls");
        ExcelListener excelListener = new ExcelListener();
        EasyExcelFactory.readBySax(inputStream, new Sheet(1, 1, Model.class), excelListener);
        inputStream.close();
    }
//    @Test
    public void saxReadSheetsV2003() throws IOException {
        InputStream inputStream = FileUtil.getResourcesFileInputStream("ceshi.xls");
        ExcelListener excelListener = new ExcelListener();
        ExcelReader excelReader = EasyExcelFactory.getReader(inputStream,excelListener);
        List<Sheet> sheets = excelReader.getSheets();
        System.out.println();
        for (Sheet sheet:sheets) {
            if(sheet.getSheetNo() == 1) {
                excelReader.read(sheet);
            }else {
                sheet.setHeadLineMun(2);
                sheet.setClazz(Model.class);
                excelReader.read(sheet);
            }
        }
        inputStream.close();
    }
//	@Test
	public void simpleReadListStringV2007() throws IOException {
		InputStream inputStream = FileUtil.getResourcesFileInputStream("ceshi.xlsx");
		ExcelListener listener = new ExcelListener();
		EasyExcelFactory.readBySax(inputStream, new Sheet(1, 1,Model.class),listener);
		inputStream.close();
	}
	public void print(List<Object> datas){
		int i=0;
		for (Object ob:datas) {
			System.out.println(i++);
			System.out.println(ob);
		}
	}
}
